<?php
include_once('config/config.php');

$id = $_GET['id'] ?? 0;

$admin = find("SELECT * FROM `pre_admin` WHERE `id` = '$id'");

if(!$admin)
{
    notice('管理员不存在');
}

if($_POST)
{
    $password = isset($_POST['password']) ? trim($_POST['password']) : '';
    $repass = isset($_POST['repass']) ? trim($_POST['repass']) : '';

    // 组装更新数据
    $data = [];

    if(!empty($password))
    {
        // 确认密码
        if(empty($repass))
        {
            notice('确认密码不能为空');
        }

        // 密码与确认密码是否不一致
        if($password != $repass)
        {
            notice('密码与确认密码不一致');
        }

        // 生成密码盐
        $salt = build_randstr();

        // 拼接密码盐然后md5加密
        $password = md5($password . $salt);

        $data['password'] = $password;
        $data['salt'] = $salt;
    }

    // 如果有更新头像
    if(isset($_FILES['avatar']) && $_FILES['avatar']['size'] > 0)
    {
        $res = build_upload('avatar');

        if($res['code'] === 0)
        {
            notice($res['msg']);
        }

        $data['avatar'] = $res['data'];
    }

    // 调用更新数据函数
    $result = update('admin',$data,"`id` = '$id'");

    // sql
    // $sql = "INSERT INTO `pre_admin`(`username`,`password`,`salt`,`avatar`)VALUES('$username','$password','$salt','$avatar')";

    // $result = mysqli_query($conn,$sql);

    if($result)
    {
        if(isset($data['avatar']) && $_FILES['avatar']['size'] > 0)
        {
            @is_file('.' . $admin['avatar']) && unlink('.' . $admin['avatar']);
        }

        notice('更新管理员成功','AdminList.php');
    }else{
        // 删除图片
        if(isset($data['avatar']) && $_FILES['avatar']['size'] > 0)
        {
            @is_file('.' . $data['avatar']) && unlink('.' . $data['avatar']);
        }

        echo $result['msg'];
        exit;

        // notice('添加管理员失败');
    }
}


?>
<!DOCTYPE html>
<html lang="en">

<head>
    <?php include_once('common/meta.php'); ?>
</head>

<body>
    <?php include_once('common/header.php'); ?>

    <?php include_once('common/menu.php'); ?>

    <div class="content">
        <div class="header">
            <h1 class="page-title">添加管理员</h1>
        </div>
        <ul class="breadcrumb">
            <li><a href="index.php">Home</a> <span class="divider">/</span></li>
            <li class="active">AdminAdd</li>
        </ul>

        <div class="container-fluid">
            <div class="row-fluid">

                <div class="btn-toolbar">
                    <button class="btn btn-primary" onClick="location='AdminList.php'"><i class="icon-list"></i> 管理员列表</button>
                    <div class="btn-group">
                    </div>
                </div>

                <div class="well">
                    <div id="myTabContent" class="tab-content">
                        <div class="tab-pane active in" id="home">
                            <form method="post" enctype="multipart/form-data">
                                <label>用户名</label>
                                <input type="text" name="username" id="username" placeholder="请输入用户名" readonly value="<?php echo $admin['username']; ?>" class="input-xxlarge">

                                <label>密码</label>
                                <input type="password" name="password" placeholder="不修改密码请留空" class="input-xxlarge">

                                <label>确认密码</label>
                                <input type="password" name="repass" placeholder="确认密码" class="input-xxlarge">

                                <label>头像</label>
                                <input type="file" id="avatar" name="avatar" hidden class="input-xxlarge">
                                <img src="<?php echo is_file('.' . $admin['avatar']) ? $admin['avatar'] : '/assets/images/avatar.png' ?>" id="img" style="width: 200px;height:200px;object-fit:cover;" alt="">

                                <label></label>
                                <input class="btn btn-primary" type="submit" value="提交" />
                            </form>
                        </div>
                    </div>
                </div>

                <?php include_once('common/footer.php'); ?>
            </div>
        </div>
    </div>

</body>

</html>

<script>
    $('#img').click(function(){
        $('#avatar').click();
    })

    $('#avatar').change(function(){
        let file = $(this)[0].files[0];

        if(file)
        {
            let Reader = new FileReader();

            Reader.readAsDataURL(file);

            Reader.onload = function()
            {
                $('#img').attr('src',Reader.result);
            }
        }
    })

    // 用户名这个输入框的值发生改变触发该事件
    $('#username').change(function(){
        let username = $.trim($(this).val());

        // 输入的用户名为空就提示
        if(!username)
        {
            $('#username-error').text('请输入用户名').css({color:'red',fontSize:'18px'});
        
            return false;
        }

        $.ajax({
            type:'post',
            url:'api.php',
            data:{
                username,
                action:'admin_username_select'
            },
            dataType:'json',
            success:(result) =>
            {
                if(result.code === 1)
                {
                    $('#username-error').text(result.msg).css({color:'green',fontSize:'18px'});
                }else{
                    $('#username-error').text(result.msg).css({color:'red',fontSize:'18px'});
                }
            }
        })
    })

    $('#username').focus(function(){
        $('#username-error').text('');
    })
</script>